using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Framework.Utils
{
    /// <summary>
    /// 一些常用文件IO操作
    /// @foldcc
    /// </summary>
    public static class FileHelper
    {
        public static string UnityRootPath = Application.persistentDataPath;

        public static bool CheckPath(string path)
        {
            return Directory.Exists(path) || File.Exists(path);
        }

        public static void CreateDirectory(string path)
        {
            if (!CheckPath(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static void DeleteDirectory(string path)
        {
            if (Directory.Exists(path))
            {
                Directory.Delete(path);
            }
        }

        public static void DeleteFile(string path, string fileName)
        {
            var fullName = path + "/" + fileName;
            if (CheckPath(fullName))
            {
                File.Delete(fullName);
            }
        }

        public static void WriteFile(string path, string fileName, byte[] content)
        {
            CreateDirectory(path);
            var fullName = path + "/" + fileName;
            FileStream fs = new FileStream(fullName, FileMode.Create , FileAccess.Write , FileShare.None , 4096 , FileOptions.None);
            fs.Write(content, 0, content.Length);
            fs.Close();
        }
        
        public static async Task WriteFileAsync(string path, string fileName, byte[] content)
        {
            CreateDirectory(path);
            var fullName = path + "/" + fileName;
            FileStream   fs = new FileStream(fullName, FileMode.Create , FileAccess.Write , FileShare.None , 4096 , FileOptions.Asynchronous);
            await fs.WriteAsync(content, 0, content.Length);
            fs.Close();
        }

        public static byte[] ReadFile(string path, string fileName)
        {
            var fullName = path + "/" + fileName;
            if (CheckPath(fullName))
            {
                FileStream fs = new FileStream(fullName, FileMode.Open , FileAccess.Read , FileShare.None , 4096 , FileOptions.None);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                return bytes;
            }
            return null;
        }

        public static byte[] StringToArrayByteUtf8(string value) { return Encoding.UTF8.GetBytes(value); }
        public static string ArrayByteUtf8ToString(byte[] bytes) { return Encoding.UTF8.GetString(bytes); }

        /// <summary> 
        /// 将 Stream 转成 byte[] 
        /// </summary> 
        public static byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary> 
        /// 将 byte[] 转成 Stream 
        /// </summary> 
        public static Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

        /// <summary> 
        /// 将 Stream 写入文件 
        /// </summary> 
        public static void StreamToFile(Stream stream, string fileName)
        {
            // 把 Stream 转换成 byte[] 
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);

            // 把 byte[] 写入文件 
            FileStream   fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        /// <summary> 
        /// 从文件读取 Stream 
        /// </summary> 
        public static Stream FileToStream(string fileName)
        {
            // 打开文件 
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            // 读取文件的 byte[] 
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            // 把 byte[] 转换成 Stream 
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

        // public static void Test()
        // {
        //
        //     string testStr = "Test";
        //     
        //     var bytes = StringToArrayByteUtf8(testStr);
        //
        //     var path = "/Test";
        //
        //     var fileName = "test1";
        //     
        //     WriteFile(path, fileName , bytes);
        //     
        //     FileInfo f = new FileInfo(path + "/" + fileName);
        //     
        //     Console.WriteLine(f.FullName);
        //
        //     var bytes2 = ReadFile(path, fileName);
        //
        //     var str2 = ArrayByteUtf8ToString(bytes2);
        //     
        //     Console.WriteLine(str2);
        //     
        //     DeleteFile(path , fileName);
        //     
        //     DeleteDirectory(path);
        // }


    }
}